<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="content-type" content="text/html; charset=utf-8" />
		<title>0/0/0</title>
		<script src="/js/jquery.js"></script>
		<style>
			body, input, textarea {
				font-size: 12px;
				font-family: 'Microsoft Yahei';
			}
			input {
				font-size: 12px;
				font-family: 'Microsoft Yahei';
			}
		</style>
	</head>
	<body>
		<table width="960" cellpadding='0' cellspacing='0'>
		<tr>
			<td height="38" valign='bottom' style="padding-left: 19px;"><input id='inState' type='button' value='发送' onclick="this.value=(this.value=='发送'?'暂停':'发送');" style='width: 110px;' /></td>
		</tr>
		<tr>
			<td height="72" valign="bottom" style="padding-left: 20px; line-height: 18px;">
				发送速度：<span id='spanSpeed'>0</span>/Min
				<br />
				发送队列：<span id='spanQueue'>0/0/0</span>
				<br />
				最近时间：<span id='spanTime'>0/0/0</span>
			</td>
		</tr>
		<tr>
			<td style="padding-left: 19px;">
				<textarea id='textHistory' cols="68" rows="10" style='margin-top: 15px; border: 0px; overflow: hidden;' readonly>投递分布：~</textarea>
				<textarea id='textHosts' cols="45" rows="10" style='margin-top: 15px; border: 0px;' readonly>主机数量：~</textarea>
			</td>
		</tr>
		<tr>
			<td style="padding-left: 19px; color: red;">
				提示：<br />
				1、以 root 身份启动脚本：<?php echo str_replace("\n", '<br />', $bin); ?>
				<br />
				2、Postfix 的 maximal_backoff_time 配置项值必须小于或等于 7200 秒。
			</td>
		</tr>
		</table>
		<script>
			var lastTime = 0;
			var inState = $('#inState')[0];
			var spanQueue = $('#spanQueue')[0];
			var spanSpeed = $('#spanSpeed')[0];
			var spanTime = $('#spanTime')[0];
			var textHistory = $('#textHistory')[0];
			var textHosts = $('#textHosts')[0];

			function interval() {
				if (inState.value == '发送') {
					// void
				} else if (new Date().getTime() - 300000 > lastTime) {
					sendmail();
				}

				setTimeout('interval()', 1000);
			}

			function sendmail() {
				lastTime = new Date().getTime();

				if (inState.value == '发送') {
					setTimeout('sendmail()', 50);

					return;
				}

				$.get("/queue/send", {"label":{$label}}, function(data, status) {
					if (status == 'success') {
						try { eval('var data = ' + data); } catch(e) {}

						if (typeof(data) == 'string' || typeof(data) == 'number') { // 弹出调试信息
							if (data) alert(data);
						} else {
							if (data.succ == data.count) {
								alert('已全部发送！');

								location.reload();
							}

							spanQueue.innerHTML = data.succ + '/' + data.fail + '/' + data.count;
							spanSpeed.innerHTML = data.speed;
							spanTime.innerHTML = new Date(lastTime).toLocaleTimeString() + "/" + new Date().toLocaleTimeString() + "/" + new Date().toLocaleTimeString();

							textHistory.value = "投递分布：";
							for (mailer in data.history) {
								textHistory.value += data.history[mailer] + "\r\n　　　　　";
							}

							if (typeof(data.hosts) != 'undefined') {
								textHosts.value = "主机数量：";
								for (host in data.hosts) {
									textHosts.value += host + "\t" + data.hosts[host] + "\r\n　　　　　";
								}
							}

							document.title = data.succ + '/' + (data.count - data.succ) + '/' + data.count;
						}
					}

					sendmail();
				});
			}

			function currentTime() {
				spanTime.innerHTML = spanTime.innerHTML.substr(0, spanTime.innerHTML.lastIndexOf('/')) + "/" + new Date().toLocaleTimeString();
			}

			window.onbeforeunload = function() { $.get("/queue-queue/destroy-1/label-{$label}") };

			interval();

			setInterval(currentTime, 250);
		</script>
	</body>
</html>